﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ADO.NET.DisconnectedClasses
{
	public class DisconnectedClassesUsage
	{
		private DataTable _myFirstTable;
		private DataTable _mySecondTable;
		private DataSet _myDataSet;
		private const string XmlFilePath = @"D:\Framework materials\Exams\Common\Data Accessing 516\PracticeProjects\testfile.xml";

		public DisconnectedClassesUsage()
		{
			_myFirstTable = new DataTable("MyFirstTable");
			_mySecondTable = new DataTable("MySecondTable");
			_myDataSet = new DataSet("MyDataSet");
		}

		public void AddDataToMyDataSet()
		{
			_myDataSet.Tables.Add(_myFirstTable);
			_myDataSet.Tables.Add(_mySecondTable);
			_myDataSet.Relations.Add("firstCol_secondCol_relation", _myDataSet.Tables["MySecondTable"].Columns["MyFirstTable_Id"],
									 _myDataSet.Tables["MyFirstTable"].Columns["Id"], true);
			ForeignKeyConstraint foreignKeyConstraint =
				(ForeignKeyConstraint)_myDataSet.Tables["MyFirstTable"].Constraints["firstCol_secondCol_relation"];
			foreignKeyConstraint.DeleteRule = Rule.Cascade;
			_myDataSet.Relations["firstCol_secondCol_relation"].Nested = true;
		}
		
		public void GetDataRowInfo(DataRow dr, string columnName)
		{
			string returnValue = "";
			foreach (string stringVersion in Enum.GetNames(typeof(DataRowVersion)))
			{
				DataRowVersion version = (DataRowVersion) Enum.Parse(typeof (DataRowVersion), stringVersion);

				if(dr.HasVersion(version))
				{
					Console.WriteLine(dr["Value", version]);
					Console.WriteLine(version);
				}
			}
		}

		public void AddDataToTable()
		{
			DataColumn idColumn = new DataColumn
			                      	{
			                      		AutoIncrement = true, 
										AutoIncrementSeed = 1, 
										AutoIncrementStep = 1, 
										ColumnName = "Id", 
										DataType = typeof(int)
			                      	};
			DataColumn nameColumn = new DataColumn
			                        	{
			                        		AllowDBNull = true,
			                        		ColumnName = "Value",
			                        		DataType = typeof (string),
											MaxLength = 50
			                        	};
			DataColumn firstTableIdColumn = new DataColumn
			                          	{
			                          		AllowDBNull = false,
											DataType = typeof(int),
											ColumnName = "MyFirstTable_Id"
			                          	};
			
			_myFirstTable.Columns.Add(idColumn);
			_myFirstTable.Columns.Add(nameColumn);
			
			_myFirstTable.PrimaryKey = new DataColumn[] { idColumn };

			_mySecondTable.Columns.Add(new DataColumn
			{
				AutoIncrement = true,
				AutoIncrementSeed = 1,
				AutoIncrementStep = 1,
				ColumnName = "Id",
				DataType = typeof(int)
			});
			_mySecondTable.Columns.Add(firstTableIdColumn);
			_mySecondTable.PrimaryKey = new DataColumn[] { _mySecondTable.Columns["Id"] } ;

			DataRow myFirstTableFirstRow = _myFirstTable.NewRow();
			myFirstTableFirstRow["Value"] = "First";
			_myFirstTable.Rows.Add(myFirstTableFirstRow);

			DataRow myFirstTableSecondRow = _myFirstTable.NewRow();
			myFirstTableSecondRow["Value"] = "Second";
			_myFirstTable.Rows.Add(myFirstTableSecondRow);

			DataRow myFirstTableThirdRow = _myFirstTable.NewRow();
			myFirstTableThirdRow["Value"] = "Third";
			_myFirstTable.Rows.Add(myFirstTableThirdRow);

			
			DataRow mySecondTableFirstRow = _mySecondTable.NewRow();
			mySecondTableFirstRow["MyFirstTable_Id"] = 1;
			_mySecondTable.Rows.Add(mySecondTableFirstRow);

			DataRow mySecondTableSecondRow = _mySecondTable.NewRow();
			mySecondTableSecondRow["MyFirstTable_Id"] = 2;
			_mySecondTable.Rows.Add(mySecondTableSecondRow);

			DataRow myThirdTableSecondRow = _mySecondTable.NewRow();
			myThirdTableSecondRow["MyFirstTable_Id"] = 3;
			_mySecondTable.Rows.Add(myThirdTableSecondRow);

			_myFirstTable.AcceptChanges();
			_mySecondTable.AcceptChanges();
		}

		public void DoStuffWithDataView()
		{
			DataView myTableDataView = new DataView(_myFirstTable);
			AddDataToTable();

			myTableDataView.Sort = "Id ASC";															
		}

		public void DoXmlStuff()
		{
			_myDataSet.WriteXml(XmlFilePath, XmlWriteMode.DiffGram);
		}
	}
}